package com.ruoyi.project.cms.pro.controller;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.pro.domain.CmsImg;
import com.ruoyi.project.cms.pro.domain.ProFile;
import com.ruoyi.project.cms.pro.service.CmsImgService;
import org.apache.tomcat.jni.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * com.ruoyi.project.cms.pro.controller
 * 作者：lh
 * 描述:
 * 时间:2020-05-21
 */
@Controller
@RequestMapping("/pro/img")
public class ImgController extends BaseController {

    private String prefix = "cms/pro";

    @Autowired
    private CmsImgService cmsImgService;

    /**
     * 文件上传
     */
    @GetMapping("/upload")
    public String upload()
    {
        return prefix + "/upload";
    }


    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("image") MultipartFile image, CmsImg img) throws IOException

    {
        System.out.println(img);
        // 上传文件路径
        String urlPath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String imgName = FileUploadUtils.upload(urlPath, image);
        img.setImgUrl(imgName);
        System.out.println(img);
        return toAjax(cmsImgService.insert(img));
    }
}
